package com.yzt.message.custom;

import android.os.Parcel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.rongyun.IMMessageType;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * 游戏消息
 */
@MessageTag(value = IMMessageType.GAME_JOIN_MESSAGE, flag = MessageTag.ISPERSISTED | MessageTag.ISCOUNTED)
public class RXJoinGameContent extends MessageContent {

    private String content;
    private String extra;
    private String ext;

    @Override
    public byte[] encode() {
        JSONObject object = new JSONObject();
        object.put("content", content);
        object.put("extra", extra);
        object.put("ext", ext);
        return object.toString().getBytes(StandardCharsets.UTF_8);

    }

    public RXJoinGameContent() {
    }

    public RXJoinGameContent(byte[] data) {
        super(data);
        String jsonStr = null;
        try {
            jsonStr = new String(data, StandardCharsets.UTF_8);
            JSONObject object = JSON.parseObject(jsonStr);

            setContent(object.getString("content"));
            setExtra(object.getString("extra"));
            setExt(object.getString("ext"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, content);
        ParcelUtils.writeToParcel(dest, extra);
        ParcelUtils.writeToParcel(dest, ext);
    }

    public static final Creator<RXJoinGameContent> CREATOR = new Creator<RXJoinGameContent>() {

        @Override
        public RXJoinGameContent createFromParcel(Parcel source) {
            return new RXJoinGameContent(source);
        }

        @Override
        public RXJoinGameContent[] newArray(int size) {
            return new RXJoinGameContent[size];
        }
    };


    public RXJoinGameContent(Parcel parcel) {
        content = ParcelUtils.readFromParcel(parcel);
        extra = ParcelUtils.readFromParcel(parcel);
        ext = ParcelUtils.readFromParcel(parcel);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }
}

